<?php

class Client_Table_Rule extends Apk_Db_Table
{
	protected $_name = 'accounts_rules';
	protected $_primary = 'id';

	protected $_dependentTables = array('Client_Table_RulePrivilege');

	protected $_referenceMap = array(
								     'Role'     => array(
            								            'columns'       => 'role_id',
            								            'refTableClass' => 'Client_Table_Role',
            								            'refColumns'    => 'id'),
	                                 'Resource' => array(
            								            'columns'       => 'resource_id',
            								            'refTableClass' => 'Client_Table_Categories',
            								            'refColumns'    => 'id'));
            								            
    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->join('accounts_roles', 'accounts_roles.id = accounts_rules.role_id', 'name')
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();		
    }
    
    public function GetRuleByRoleRes($role_id, $resource_id)
    {    	
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('role_id = ?', $role_id)
    				->where('resource_id = ?', $resource_id);    	
    				
    	return $this->getList($sel)->toArray();		
    }  
    
    public function GetPrivByRule($rule_id)
    {
    	$sel = $this->getAdapter()->select()
    				->from('accounts_rules_privileges')
    				->join('accounts_privileges', 'accounts_privileges.id = accounts_rules_privileges.privilege_id')
    				->where('rule_id = ?', $rule_id);
    				    				
    	return $this->getList($sel)->toArray();		
    } 
    
    public function GetRuleList()
	{
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->join('categories', 'categories.id = accounts_rules.resource_id', array('site', 'module', 'controller'))
    				->join('accounts_roles', 'accounts_roles.id = accounts_rules.role_id', 'name')
    				->where('categories.is_active');    				
    				
        return $this->getList($sel)->toArray();				
	}
	
    public function GetPrivilegeList($rule_id)
	{
    	$sel = $this->getAdapter()->select()
    				->from('accounts_rules_privileges')
    				->joinLeft('accounts_privileges', 'accounts_privileges.id = accounts_rules_privileges.privilege_id', 'name')
    				->where('accounts_rules_privileges.rule_id = ?', $rule_id);    	
    	if($rule_id == 135) 
    	{
    		echo $sel->assemble();						
    		die();
    	}
    				
        return $this->getList($sel)->toArray();				
	} 	
    
	public function GetRoleList($id_res)
	{
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->join('accounts_roles', 'accounts_roles.id = accounts_rules.role_id', array('name', 'name_full'))
    				->where('accounts_rules.resource_id = ?', $id_res)
    				->group('accounts_roles.name');    
    								
    	$data =  $this->getList($sel)->toArray();
    	
    	foreach($data as $k => $item)
    	{
    		$sel = $this->getAdapter()->select()
    						->from('accounts_rules_privileges')
    						->join('accounts_privileges', 'accounts_rules_privileges.privilege_id = accounts_privileges.id')
    						->where('accounts_rules_privileges.rule_id = ?', $item['id']);
    						
    		$data['privileges'] = $this->getList($sel)->toArray();
    	}
    	
    	return $data;
	}

    public function insert(array $data, $user_id)
    {    	
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	$data['type'] = 'deny';
        return parent::insert($data);
    }

    public function update(array $params, $resource_id, $role_id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['modify_user_id'] = $user_id;    	
    	$data['add_user_id'] = $user_id;   	    	
    	$data['role_id'] = $role_id;
    	$data['resource_id'] = $resource_id;    	
    	
		//parent::delete('role_id = '.$role_id.' AND resource_id = '.$resource_id);		
		$this->remove(array('role_id' => $role_id, 'resource_id' => $resource_id));
		
		foreach($params as $k => $param)		
		{ 
			$data['type'] = $k;
			$rule_id = parent::insert($data);
			
			foreach($param as $j => $privileges_id)
			{
				$this->getAdapter()->insert('accounts_rules_privileges', array('rule_id' => $rule_id, 'privilege_id' => $privileges_id));
			}
		}			
		
		return true;		
    }

    public function remove($data)
    {    	
    	$rules = $this->GetRuleByRoleRes($data['role_id'], $data['resource_id']);
    	foreach($rules as $k => $item)
    	{
    		$this->getAdapter()->delete('accounts_rules_privileges', 'rule_id = '.$item['id']);
    	}
    	
    	parent::delete('role_id = '.$data['role_id'].' AND resource_id = '.$data['resource_id']);
    	return true;    	
	}
}
?>